package com.zjpavt.client.page;

import com.zjpavt.client.hal.DogWatch;
import com.zjpavt.client.hal.LedScreen;
import com.zjpavt.client.page.abstractpage.AbstractBasePage;
import com.zjpavt.client.page.basepage.BaseEditablePage;
import com.zjpavt.client.page.basepage.BaseSonPage;
import com.zjpavt.client.page.editableunit.EditUnit;
import com.zjpavt.client.page.editableunit.EditUnitString;
import com.zjpavt.client.page.intefacepage.EditablePage;
import com.zjpavt.client.page.intefacepage.ParentPage;
import com.zjpavt.client.page.intefacepage.SonPage;
import com.zjpavt.client.page.pageunit.ScreenLine;

import java.util.ArrayList;
import java.util.List;

/**
 * 开门狗的显示页面
 *
 * @author zyc
 */
public class DogWatchPage extends AbstractBasePage implements SonPage, EditablePage {

    private SonPage sonPage;
    private EditablePage editablePage;
    /**
     *     初始化的值从文件中获取
     */
    private boolean isDogWatchUsed = DogWatch.getInstance().isDogWatchUsed();

    public DogWatchPage(int pageNum, ParentPage parentPage, long mRefreshPeriod) {
        super(pageNum,mRefreshPeriod);
        sonPage = new BaseSonPage(getPageNum(), parentPage,0, this);
        editablePage = new BaseEditablePage(getPageNum(), 0,this);
    }

    /**
     * 编辑的时候 缓存可编辑单元中的值 是否使用开门狗
     *
     * @return
     */
    public boolean getDogWatchUsedWhenEdit() {
        return Boolean.parseBoolean(getEditUnit(0).getText());
    }

    @Override
    public void refreshPage() {
        List<ScreenLine> result = new ArrayList<>();
        result.add(new ScreenLine.ScreenLineBuilder().text("dogWatch").pageNum(getPageNum()).build());
        result.add(new ScreenLine.ScreenLineBuilder().text(String.valueOf(isDogWatchUsed)).build());
        result.add(new ScreenLine.ScreenLineBuilder().text("").build());
        result.add(new ScreenLine.ScreenLineBuilder().text("").build());
        LedScreen.getInstance().setText(result);
    }

    /********************************************************
     * sonpage
     * @return
     */
    @Override
    public ParentPage getParentPage() {
        return sonPage.getParentPage();
    }


    @Override
    public void enterSonPage() {
        refreshPage();
    }

    @Override
    public void exitSonPage() {
        if (isInEditMode()) {
            throw new IllegalArgumentException();
        }
        sonPage.exitSonPage();
    }

    @Override
    public AbstractBasePage setParentPage(ParentPage basePageImpl) {
        sonPage.setParentPage(basePageImpl);
        return this;
    }

    @Override
    public void close() {

    }

    /*************************************************
     * editpage
     */
    @Override
    public void addEditUnit(EditUnit e) {
        editablePage.addEditUnit(e);
    }

    @Override
    public void removeEditUnit(EditUnit e) {
        editablePage.removeEditUnit(e);
    }

    @Override
    public void clearEditUnit() {
        editablePage.clearEditUnit();
    }

    @Override
    public EditUnit getEditUnit(int index) {
        return editablePage.getEditUnit(index);
    }

    @Override
    public int getCurrBlinkIndex() {
        return editablePage.getCurrBlinkIndex();
    }

    @Override
    public void exitEditMode() {
        editablePage.exitEditMode();
    }

    @Override
    public boolean isInEditMode() {
        return editablePage.isInEditMode();
    }

    @Override
    public int getEditUnitSize() {
        return editablePage.getEditUnitSize();
    }

    @Override
    public void enterEditMode() {
        this.editablePage.enterEditMode();
    }

    @Override
    public void setDefaultVal() {
        int iIsWatchIndex = isDogWatchUsed ? 1 : 0;
        getEditUnit(0).setCurrentVal(iIsWatchIndex);
    }

    @Override
    public void initEditUnits() {
        int iIsWatchIndex = isDogWatchUsed ? 1 : 0;
        addEditUnit(new EditUnitString(2, 1, 5, new String[] {"false", "true"}, iIsWatchIndex));
    }

    @Override
    public void saveVal() {
        isDogWatchUsed = getDogWatchUsedWhenEdit();
        DogWatch.getInstance().saveDogWatchStatus(isDogWatchUsed);
    }

    @Override
    public void init() {
        initEditUnits();
    }

    /******************************************************
     * basePage
     */
    @Override
    public void onKeyDown() {
        if (isInEditMode()) {
            editablePage.onKeyDown();
        } else {
            super.onKeyDown();
        }
    }

    @Override
    public void onKeyUp() {
        if (isInEditMode()) {
            editablePage.onKeyUp();
        } else {
            super.onKeyUp();
        }
    }

    @Override
    public void onKeySet() {
        if (isInEditMode()) {
            editablePage.onKeySet();
        } else {
            enterEditMode();
        }
    }

    /**
     * 在编辑模式下 确认时 退出编辑模式
     * 如果不在编辑下 退出当前的页面
     */
    @Override
    public void onKeyEnt() {
        if (isInEditMode()) {
            exitEditMode();
        } else {
            exitSonPage();
        }
    }
}
